package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.utils.DecimalUtil;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Order;
import org.springblade.modules.app.entity.OrderComments;
import org.springblade.modules.app.service.ICoalService;
import org.springblade.modules.app.service.IOrderCommentsService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 工单信息表 控制器
 *
 * @author BladeX
 * @since 2020-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/ordercomments")
@Api(value = "工单信息表", tags = "工单信息表接口")
public class OrderCommentsController extends BladeController {

    private final IOrderCommentsService orderCommentsService;
    private final ICoalService orderService;
    private final IUserService userService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入orderComments")
    public R<OrderComments> detail(OrderComments orderComments) {
        OrderComments detail = orderCommentsService.getOne(Condition.getQueryWrapper(orderComments));
        return R.data(detail);
    }

    /**
     * 分页 工单信息表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入orderComments")
    public R<IPage<OrderComments>> list(OrderComments orderComments, Query query) {
        IPage<OrderComments> pages = orderCommentsService.page(Condition.getPage(query), Condition.getQueryWrapper(orderComments));
        return R.data(pages);
    }

    /**
     * 自定义分页 工单信息表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入orderComments")
    public R<IPage<OrderComments>> page(Page page, OrderComments orderComments) {
        BladeUser bladeUser = AuthUtil.getUser();
        LambdaQueryWrapper<OrderComments> queryWrapper = new LambdaQueryWrapper();
        if ("0".equals(orderComments.getPjstatus())) {
            queryWrapper.and(wrapper -> wrapper.eq(OrderComments::getPjstatus, "1").or().eq(OrderComments::getPjstatus, "2"));
        } else {
            queryWrapper.and(wrapper -> wrapper.eq(OrderComments::getPjstatus, orderComments.getPjstatus()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(OrderComments::getWorkerid, bladeUser.getUserId()));
        queryWrapper.orderByDesc(OrderComments::getCtime);
        IPage<OrderComments> pages = orderCommentsService.page(page, queryWrapper);
        return R.data(pages);
    }


    /**
     * 新增 工单信息表
     */
    @PostMapping("/save")
    public R save(@RequestBody OrderComments orderComments) {
        BladeUser bladeUser = AuthUtil.getUser();
        User merchantUserInfo = userService.getById(bladeUser.getUserId());
        Order orderInfo = orderService.getById(orderComments.getOrderid());
        orderComments.setCuserid(orderInfo.getCuserid() + "");
        orderComments.setCusername(orderInfo.getCusername());
        orderComments.setCuserphone(orderInfo.getCuserphone());
        orderComments.setCuserphoto(merchantUserInfo.getAvatar());
        orderComments.setWorkerid(orderInfo.getWorkerid() + "");
        orderComments.setWorkername(orderInfo.getWorkername());
        orderComments.setWorkerphone(orderInfo.getWorkerphone());
        if (orderComments.getPjstart() <= 3) {
            orderComments.setPjstatus("2");
        } else {
            orderComments.setPjstatus("1");
        }
        orderComments.setCtime(new Date());
        boolean save = orderCommentsService.save(orderComments);
        updateManyiDu(orderInfo.getWorkerid());
        return R.status(save);
    }

    public void updateManyiDu(String workerid) {
        User workerUserInfo = userService.getById(workerid);

        LambdaQueryWrapper<OrderComments> queryWrapperMY = new LambdaQueryWrapper();
        queryWrapperMY.and(wrapper -> wrapper.eq(OrderComments::getPjstatus, "1"));
        queryWrapperMY.and(wrapper -> wrapper.eq(OrderComments::getWorkerid, workerUserInfo.getId()));
        int MyCount = orderCommentsService.count(queryWrapperMY);

        LambdaQueryWrapper<OrderComments> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(wrapper -> wrapper.eq(OrderComments::getWorkerid, workerUserInfo.getId()));
        int totleCount = orderCommentsService.count(queryWrapper);

        BigDecimal percent = DecimalUtil.divide(new BigDecimal(MyCount), new BigDecimal(totleCount));
        BigDecimal multiply = percent.multiply(new BigDecimal("100"));
        String workdermyd = multiply.toString() + "%";

        User info = new User();
        info.setId(workerUserInfo.getId());
        info.setWorkermyd(workdermyd);
        userService.updateById(info);

    }


    /**
     * 修改 工单信息表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入orderComments")
    public R update(@Valid @RequestBody OrderComments orderComments) {
        return R.status(orderCommentsService.updateById(orderComments));
    }

    /**
     * 新增或修改 工单信息表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入orderComments")
    public R submit(@Valid @RequestBody OrderComments orderComments) {
        return R.status(orderCommentsService.saveOrUpdate(orderComments));
    }


    /**
     * 删除 工单信息表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(orderCommentsService.removeByIds(Func.toLongList(ids)));
    }


}
